#include "stdafx.h"
#include "ctl_panel.h"
#include "config.h"

void panel::add_attribute(wstring aStrName, wstring aStrValue)
{
	if (aStrName.compare(L"config") == 0)
	{
		m_strConfig = aStrValue;
		return;
	}
	__super::add_attribute(aStrName, aStrValue);
}

void panel::on_clone_attributes(control* aSrc)
{
	
}

bool panel::load(IXMLDOMNode* aXmlNode)
{
	if (!__super::load(aXmlNode))
	{
		return false;
	}
	return true;
}

bool panel::save(IXMLDOMNode* aXmlNode)
{
	IXMLDOMElement*		pXmlElement = (IXMLDOMElement*) aXmlNode;
	if (m_strConfig.length() > 0)
	{
		pXmlElement->setAttribute(L"config",		(_variant_t)m_strConfig.c_str());
	}
	return true;
}

void panel::render(Graphics & aGraphics){

}

void panel::render_after(Graphics & aGraphics)
{
	if (m_strConfig.length()>0 && m_config)
	{
		__super::render_after(aGraphics);
	}
}

void panel::on_focused()
{
	if (!m_config)
	{
		for (uint i = 0; i < m_childs.size(); i++)
		{
			m_childs[i]->on_focused();
		}

		wstring configFile;
		if (m_belongTo)
			configFile = path_helper::process_file(m_strConfig, m_belongTo->get_file_name());
		else
			configFile = path_helper::process_file(m_strConfig);
		data_xml* xml = data_xml::load_xml(configFile);
		m_config = PsDataXml(xml);
		if (xml)
		{
			control* ctls = xml->m_controls.m_controlTree->clone();
			m_pos	= ctls->m_pos;
			m_size	= ctls->m_size;
			m_align = ctls->m_align;

			//ctls->m_bIsReferenced = true;

			for (uint i = 0; i < ctls->m_childs.size(); i++)
			{
				ctls->m_childs[i]->m_bIsReferenced = true;
			}

			push_back(ctls);
			
			s_data_event e;
			e.m_sender = m_belongTo;
			e.m_event_type = EDO_CTL_TREE_CHANGED;
			//e.m_arg = ctl;
			m_belongTo->notify_event(e);	
		}
	}	
}
